using System;
using gnodotnet.Core.MailingList;
using SharpArch.Core.PersistenceSupport;
using gnodotnet.Core;

namespace gnodotnet.ApplicationServices {
    public class RecipientRegistration {
        private IRepository<Recipient> _recipientRepository;
        public RecipientRegistration(IRepository<Recipient> recipientRepository) {
            _recipientRepository = recipientRepository;
        }
        public ServiceResult AttemptRegistration(Recipient recipient) {
            if (_recipientRepository.FindOne(new EntitySpec<Recipient>().With(r => r.Name, recipient.Name).ToD()).IsNotNull())
                return new ServiceResult() { Errors = "{0} was previously subscribed to the list".Use(recipient.Email).InList() };
            _recipientRepository.SaveOrUpdate(recipient);
            return new ServiceResult() { Notifications = "{0} has been subscribed to the list".Use(recipient.Email).InList() };
        }
    }
}
